﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PH.Model.Schools;
using PH.Data.Schools;
using PH.Logic.Commons;
using System.Collections;
using System.Text.RegularExpressions;
namespace PH.Logic.Schools
{
    public class ClassLogic
    {
        public static ClassModel CreateNewClass(ClassModel model)
        {

            return ClassCommand.CreateNewClass(model);
        }

        public static void UpdateClass(ClassModel model)
        {
            ClassCommand.UpdateClass(model);
        }

        public static void DeleteClass(Guid guid)
        {
            ClassCommand.DeleteClass(guid);
        }

        public static ClassModel GetClass(Guid guid)
        {
            return ClassCommand.GetClass(guid);
        }

        public static ClassCollection GetClasses(ClassQuery search)
        {
            return ClassCommand.GetClasses(search);
        }

        /// <summary>
        /// 导入学生资料
        /// </summary>
        /// <param name="fileGuid"></param>
        /// <param name="schoolGuid"></param>
        /// <param name="errorFileGuid"></param>
        /// <returns></returns>
        public static Guid ImportClass(Guid fileGuid, Guid schoolGuid)
        {
            var workbook = FileLogic.GetExcel2003(fileGuid);
            Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
            cells.DeleteBlankColumns();
            cells.DeleteBlankRows();
            var nc = 3;
            cells[0, nc].PutValue("错误信息");

            var i = 1;
            var rows = cells.Rows.Count - 1;
            var nowrow = 0;
            FileLogic.UpdateFile(fileGuid, 0, rows);
            while (i < cells.Rows.Count)
            {
                try
                {
                    nowrow++;
                    FileLogic.UpdateFile(fileGuid, nowrow);

                    var model = new ClassModel()
                    {
                        SchoolGuid = schoolGuid,
                        GradeCode = Convert.ToInt32(cells[i, 1].Value),
                        CIndex = Convert.ToInt32(cells[i, 0].Value)
                    };
                    if (cells[i, 2].Value != null)
                        model.Advisor = cells[i, 2].Value.ToString();
                    if (cells[i, 3].Value != null)
                        model.ClassName = cells[i, 3].Value.ToString();

                    ClassCommand.CreateNewClass(model);
                    cells.DeleteRow(i);
                }
                catch (Exception ex)
                {
                    cells[i, nc].PutValue(ex.Message);
                    i++;
                }
            }

            if (cells.Rows.Count > 1)
            {
                var errFile = FileLogic.SaveExcel2003ToFile(workbook, "导入班级的错误记录.xls");
                return errFile.Guid;
            }
            return Guid.Empty;
        }
        /// <summary>
        /// 建立外籍班级
        /// </summary>
        /// <param name="schGuid"></param>
        public static void CreateForignClass(Guid schGuid)
        {
            ClassCommand.CreateForignClass(schGuid);
        }
    }
}
